//
//  UILabel.swift
//  readtool
//
//  Created by WanJiading on 16/6/6.
//  Copyright © 2016年 DAD. All rights reserved.
//

import UIKit

extension UILabel {
    // The output below is limited by 1 KB.
    // Please Sign Up (Free!) to remove this limitation.

    convenience init(fontSize: CGFloat, color: UIColor) {
        self.init()
        self.textColor = color
        self.font = UIFont.systemFont(ofSize: fontSize)
    }


    convenience init(fontSize: CGFloat, color: UIColor, backgroundColor: UIColor?) {
        self.init(fontSize: fontSize, color: color)
        if backgroundColor != nil {
            self.backgroundColor = backgroundColor
        }
    }

    convenience init(fontSize: CGFloat, color: UIColor, backgroundColor: UIColor?, textAlignment: NSTextAlignment) {
        self.init(fontSize: fontSize, color: color, backgroundColor:  backgroundColor)
        self.textAlignment = textAlignment
    }

    convenience init(text: String, fontSize: CGFloat, color: UIColor, backgroundColor: UIColor?, textAlignment: NSTextAlignment) {
        self.init(fontSize: fontSize, color: color, backgroundColor: backgroundColor, textAlignment: textAlignment)
        self.text = text
    }

    /**
     *  设置行间距
     */
    func setLinespace(_ lineSpace: CGFloat) {
        if (self.text == nil || self.text!.count == 0) {
            return
        }
        let attributedString = NSMutableAttributedString(string: self.text!)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpace
        attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, self.text!.count))
        self.attributedText = attributedString
    }
}
